/*
 * @Descripttion:
 * @version:
 * @Author: sueRimn
 * @Date: 2020-10-20 17:48:29
 * @LastEditors: sueRimn
 * @LastEditTime: 2020-11-16 10:36:18
 */
import router from './router'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getSessionInfo, delSessionInfo } from '@/utils/auth' // get token
import store from './store'
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login']

router.beforeEach(async (to, from, next) => {
    // start progress bar
    NProgress.start()

    // set page title
    document.title = getPageTitle(to.meta.title)

    // 拦截判断验证
    const hasToken = JSON.parse(getSessionInfo())

    // 先判断是否有vuex
    if (store.getters.k_token) {
        if (store.getters.permission_routes.length != 0) {
            next()
        } else {
            let roles = store.getters.sideBarRoles
            console.log('拿到权限1：', roles)
            const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
            router.addRoutes(accessRoutes)
            next(to.path)
        }
    } else if (hasToken) {
        try {
            await store.dispatch('user/login', hasToken).then(async res => {
                if (res.code == 200) {
                    let roles = store.getters.sideBarRoles
                    console.log('拿到Roles2', roles)
                    const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
                    router.addRoutes(accessRoutes)
                    next(to.path)
                } else {
                    Message.error('登录失败')
                    next(`/login`)
                    delSessionInfo()
                    location.reload()
                }
            })
        } catch (error) {
            Message.error('登录失败')
            next(`/login`)
            NProgress.done()
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            // in the free login whitelist, go directly
            next()
        } else {
            // other pages that do not have permission to access are redirected to the login page.
            next(`/login`)
            NProgress.done()
        }
    }

    // if (to.path === '/404') {
    //     next()
    // } else if (store.getters.k_token) {
    //     next()
    // } else if (to.query && to.query.k_token) {
    //     setToken(to.query.k_token)
    //     var isheadUrl = to.query.headUrl || require('@/assets/logo/logo.png')
    //     setHeadUrlToken(isheadUrl)
    //     store.commit('user/SET_AVATAR', isheadUrl)
    //     store.commit('user/SET_K_TOKEN', to.query.k_token)
    //     get_user()
    //     next()
    // } else if (hasToken) {
    //     var isheadUrlToken = getHeadUrlToken() || require('@/assets/logo/logo.png')
    //     store.commit('user/SET_AVATAR', isheadUrlToken)
    //     store.commit('user/SET_K_TOKEN', hasToken)
    //     get_user()
    //     next()
    // } else {
    //     Message.error('获取登录授权失败')
    //     next('/404')
    // }
})

router.afterEach(() => {
    // finish progress bar
    NProgress.done()
})
